﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Extensions on the <see cref="string" /> class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Text;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// Extensions on the <see cref="string"/> class.
	/// </summary>
	public static class StringExtensions
	{
		/// <summary>
		/// Converts a string to it's base 64 encoded representation.
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <returns>The value as a base 64 encoded <see cref="string"/></returns>
		/// <remarks>The reverse is <seealso cref="FromStorageMetadataSafeValue"/>.</remarks>
		public static string ToStorageMetadataSafeValue(this string value)
		{
			return Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
		}

		/// <summary>
		/// Converts a string from it's base 64 encoded representation back to a regular <see cref="string"/>
		/// </summary>
		/// <param name="value">The value to convert</param>
		/// <returns>The value as a regular <see cref="string"/></returns>
		/// <remarks>The reverse is <seealso cref="ToStorageMetadataSafeValue"/>.</remarks>
		public static string FromStorageMetadataSafeValue(this string value)
		{
			return Encoding.UTF8.GetString(Convert.FromBase64String(value));
		}
	}
}